package Day14;

import jdk.nashorn.internal.scripts.JO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TransferPanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel transferAccountLabel = new JLabel("账号：");
    private JLabel transferMoneyLabel = new JLabel("金额：");
    private JTextField accountField = new JTextField();
    private JTextField moneyField = new JTextField();
    private Font font = new Font("宋体", Font.BOLD, 20);
    private JButton sureBtn = new JButton(new ImageIcon("Image/buttonSure.jpg"));
    private JButton cancelBtn = new JButton(new ImageIcon("Image/buttonOff.jpg"));

    public TransferPanel(ATM05 atm05) {
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        transferAccountLabel.setBounds(300, 200, 80, 50);
        transferAccountLabel.setFont(font);
        this.add(transferAccountLabel);

        transferMoneyLabel.setBounds(300, 250, 80, 50);
        transferMoneyLabel.setFont(font);
        this.add(transferMoneyLabel);

        accountField.setBounds(380, 200, 250, 50);
        accountField.setFont(font);
        this.add(accountField);

        moneyField.setBounds(380, 250, 250, 50);
        moneyField.setFont(font);
        this.add(moneyField);

        sureBtn.setBounds(370, 320, 140, 50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        cancelBtn.setBounds(520, 320, 140, 50);
        cancelBtn.addActionListener(this);
        this.add(cancelBtn);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            String otherAccount = accountField.getText();
            try {
                double blance = Double.parseDouble(moneyField.getText());
                User loginUser = atm05.getLoginUser();
                ArrayList<User> users = atm05.getUsers();
                if(loginUser.getAccount().equals(otherAccount)){
                    JOptionPane.showMessageDialog(this,"不能转给自己");
                    accountField.setText("");
                    moneyField.setText("");
                    return;
                }
                boolean flag = false;
                int otherIndex = 0;
                for(int i = 0;i < users.size();i++){
                    if(otherAccount.equals(users.get(i).getAccount())){
                        flag = true;
                        otherIndex = i;
                        break;
                    }
                }
                if(!flag){
                    JOptionPane.showMessageDialog(this,"账号不存在");
                    accountField.setText("");
                    moneyField.setText("");
                    return;
                }
                if(loginUser.getBlance() < blance){
                    JOptionPane.showMessageDialog(this,"余额不足");
                    accountField.setText("");
                    moneyField.setText("");
                    return;
                }
                loginUser.setBlance(loginUser.getBlance() - blance);
                users.get(otherIndex).setBlance(users.get(otherIndex).getBlance() + blance);
                fileUtil.writeObject(users,atm05.getFILE_PATH());
                JOptionPane.showMessageDialog(this,"转账成功");
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"金额只能输入数字");
                moneyField.setText("");
                accountField.setText("");
            }
            moneyField.setText("");
            accountField.setText("");
            atm05.showPanel("menu");
        }
        if (e.getSource() == cancelBtn){
            accountField.setText("");
            moneyField.setText("");
            atm05.showPanel("menu");
        }
    }
}
